package com.dylon.seapiweb.controller;

import com.dylon.common.base.AjaxResult;
import com.dylon.common.utils.StringUtils;
import com.dylon.system.domain.SysModule;
import com.dylon.system.domain.SysProject;
import com.dylon.system.service.ISysModuleService;
import com.dylon.system.service.ISysProjectService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 模块信息
 */
@Controller
@RequestMapping("/system/module")
@Log4j2
public class SysModuleController extends BaseController{
    private String prefix = "system/module";

    @Autowired
    private ISysModuleService moduleService;
    @Autowired
    private ISysProjectService projectService;

    @RequiresPermissions("system:module:view")
    @GetMapping()
    public String module()
    {
        return prefix + "/module";
    }

    @RequiresPermissions("system:module:list")
    @GetMapping("/list")
    @ResponseBody
    public List<SysModule> list(SysModule module){
        List<SysProject> projectList = projectService.selectProjectList(new SysProject());
        List<SysModule> list = new ArrayList<>();
        projectList.forEach(project -> {
            SysModule projectModule = new SysModule();
            projectModule.setModuleId(project.getProjectId());
            projectModule.setModuleName(project.getProjectName()+"(项目)");
            projectModule.setParentId(0L);
            projectModule.setOrderNum(project.getOrderNum());
            projectModule.setStatus(project.getStatus());
            list.add(projectModule);
        });

        List<SysModule> moduleList = moduleService.selectModuleList(module);
        moduleList.forEach(module1 -> {
            if(module1.getParentId()==0L){
                module1.setParentId(module1.getProjectId());
            }
        });

        list.addAll(moduleList);
        return list;
    }

    /**
     * 新增模块
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap){
        Long projectId = null;
        String parentName = "";
        if(StringUtils.isNotNull(parentId) && 0!=parentId){
            SysModule parent = moduleService.selectModuleById( parentId );
            parentName = parent.getModuleName();
            projectId = parent.getProjectId();
        }
        mmap.put("parentId", parentId);
        mmap.put("parentName", parentName);
        mmap.put("projectId", projectId);
        mmap.put("projects", projectService.selectProjectList(new SysProject()));
        return prefix + "/add";
    }

    /**
     * 新增保存模块
     */
    @RequiresPermissions("system:module:add")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(SysModule module){
        module.setCreateBy(getSysUser().getUserName());
        return toAjax(moduleService.insertModule(module));
    }

    /**
     * 修改模块
     */
    @GetMapping("/edit/{moduleId}")
    public String edit(@PathVariable("moduleId") Long moduleId, ModelMap mmap){
        SysModule module = moduleService.selectModuleById(moduleId);
        if (StringUtils.isNotNull(module) && 0L == module.getParentId()){
            module.setParentName("无");
        }
        mmap.put("module", module);
        return prefix + "/edit";
    }

    /**
     * 修改保存模块
     */
    @RequiresPermissions("system:module:edit")
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(SysModule module){
        module.setUpdateBy(getSysUser().getUserName());
        return toAjax(moduleService.updateModule(module));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:module:remove")
    @PostMapping("/remove/{moduleId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("moduleId") Long moduleId){
        try{
            return toAjax(moduleService.deleteModuleById(moduleId));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 校验部门名称
     */
    @PostMapping("/checkModuleNameUnique")
    @ResponseBody
    public String checkModuleNameUnique(SysModule module){
        return moduleService.checkModuleNameUnique(module);
    }

    /**
     * 模块状态修改
     */
    @RequiresPermissions("system:module:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysModule module)
    {
        return toAjax(moduleService.changeStatus(module));
    }

    /**
     * 加载模块列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Map<String, Object>> treeData(){
        List<Map<String, Object>> tree = moduleService.selectModuleTree(new SysModule());
        return tree;
    }

    /**
     * 选择模块树
     */
    @GetMapping("/selectModuleTree/{moduleId}")
    public String selectDeptTree(@PathVariable("moduleId") Long moduleId, ModelMap mmap){
        if(moduleId==0){
            List<SysModule> modules = moduleService.selectModuleList(new SysModule());
            mmap.put("module", modules.get(0));
        }else{
            mmap.put("module", moduleService.selectModuleById(moduleId));
        }
        return prefix + "/tree";
    }
}
